package com.project.weather.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.project.weather.common.AResultCode;
import com.project.weather.common.Constant;
import com.project.weather.common.ResultMap;
import com.project.weather.pojo.api.WeatherLiveResponsePojo;
import com.project.weather.pojo.api.WeatherRequestPojo;
import com.project.weather.pojo.api.WeatherSevendaysResponsePojo;
import com.project.weather.service.WeatherService;
import com.project.weather.utils.StringUtil;
import com.project.weather.utils.http.HttpUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 *<p>Title:UserServiceImpl.java</p>
 *<p>Description:查询天气ServiceImpl</p>
 *@author xxx
 *@version 1.0
 *@Automatically generate by Coder in 2021-12-21 16:07
 */
@Service
public class WeatherServiceImpl implements WeatherService {
    @Override
    public ResultMap<AResultCode, WeatherLiveResponsePojo> searchLive(WeatherRequestPojo bean) {
        ResultMap<AResultCode, WeatherLiveResponsePojo> result = new ResultMap<>();


        bean.setVersion(Constant.WEATHERAPIVERSION6);
        bean.setAppid(Constant.WEATHERAPIAPPID);
        bean.setAppsecret(Constant.WEATHERAPIAPPSECRET);
        bean.setUnescape(1);


        StringBuffer params = new StringBuffer();
        params.append("appid=").append(bean.getAppid());

        params.append("&").append("version=").append(bean.getVersion());

        //数字签名，###填你的数字签名，可以在你的个人中心看到
        params.append("&").append("appsecret=").append(bean.getAppsecret());

        params.append("&").append("unescape=").append(bean.getUnescape());

        if(!StringUtil.isNull(bean.getCallback())){
            params.append("&").append("callback=").append(bean.getCallback());
        }

        if(!StringUtil.isNull(bean.getCity())){
            params.append("&").append("city=").append(bean.getCity());
        }

        if(!StringUtil.isNull(bean.getCityid())){
            params.append("&").append("cityid=").append(bean.getCityid());
        }

        if(!StringUtil.isNull(bean.getIp())){
            params.append("&").append("ip=").append(bean.getIp());
        }

        if(!StringUtil.isNull(bean.getVue())){
            params.append("&").append("vue=").append(bean.getVue());
        }

        

        ObjectMapper om = new ObjectMapper();

        try {
            String s = HttpUtils.sendGet(Constant.WEATHERAPIURL, params.toString());
            WeatherLiveResponsePojo weatherLiveResponsePojo = om.readValue(s, WeatherLiveResponsePojo.class);
            result.setResult(weatherLiveResponsePojo);
            result.setCode(AResultCode.OK);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(AResultCode.FAIL);
        }
        return result;
    }

    @Override
    public ResultMap<AResultCode, WeatherSevendaysResponsePojo> searchSevenDays(WeatherRequestPojo bean) {
        ResultMap<AResultCode, WeatherSevendaysResponsePojo> result = new ResultMap<>();


        bean.setVersion(Constant.WEATHERAPIVERSION1);
        bean.setAppid(Constant.WEATHERAPIAPPID);
        bean.setAppsecret(Constant.WEATHERAPIAPPSECRET);
        bean.setUnescape(1);


        StringBuffer params = new StringBuffer();
        params.append("appid=").append(bean.getAppid());

        params.append("&").append("version=").append(bean.getVersion());

        //数字签名，###填你的数字签名，可以在你的个人中心看到
        params.append("&").append("appsecret=").append(bean.getAppsecret());

        params.append("&").append("unescape=").append(bean.getUnescape());

        if(!StringUtil.isNull(bean.getCallback())){
            params.append("&").append("callback=").append(bean.getCallback());
        }

        if(!StringUtil.isNull(bean.getCity())){
            params.append("&").append("city=").append(bean.getCity());
        }

        if(!StringUtil.isNull(bean.getCityid())){
            params.append("&").append("cityid=").append(bean.getCityid());
        }

        if(!StringUtil.isNull(bean.getIp())){
            params.append("&").append("ip=").append(bean.getIp());
        }

        if(!StringUtil.isNull(bean.getVue())){
            params.append("&").append("vue=").append(bean.getVue());
        }



        ObjectMapper om = new ObjectMapper();

        try {
            String s = HttpUtils.sendGet(Constant.WEATHERAPIURL, params.toString());
            WeatherSevendaysResponsePojo weatherSevendaysResponsePojo = om.readValue(s, WeatherSevendaysResponsePojo.class);
            result.setResult(weatherSevendaysResponsePojo);
            result.setCode(AResultCode.OK);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(AResultCode.FAIL);
        }
        return result;
    }
}
